
package com.hlkj.pay.app.merchant.ext.impl.wxpay.ext;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.merchant.ext.MerchantExtConstant;
import com.hlkj.pay.app.merchant.ext.dto.order.ChannelPayOrderQueryContext;
import com.hlkj.pay.app.merchant.ext.dto.order.ChannelRefundPayOrderContext;
import com.hlkj.pay.app.merchant.ext.impl.wxpay.constant.WxPayExtConstant;
import com.hlkj.pay.app.merchant.ext.impl.wxpay.dto.WxAppChannelParams;
import com.hlkj.pay.app.merchant.ext.impl.wxpay.dto.WxNotifyResp;
import com.hlkj.pay.app.merchant.ext.impl.wxpay.dto.WxPayConfig;
import com.hlkj.pay.app.merchant.ext.impl.wxpay.dto.WxRefundOrderResp;
import com.hlkj.pay.app.merchant.ext.impl.wxpay.handler.WxPayCommonParamsHandle;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.enums.PayOrderEnums;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAppChannelDO;
import com.hlkj.pay.infrastructure.model.order.PayOrderDO;
import com.hlkj.pay.infrastructure.model.order.RefundOrderDO;
import com.hlkj.pay.util.DateUtils;
import com.hlkj.pay.util.MerchantUtils;
import com.ijpay.core.IJPayHttpResponse;
import com.ijpay.core.enums.AuthTypeEnum;
import com.ijpay.core.enums.RequestMethodEnum;
import com.ijpay.core.kit.WxPayKit;
import com.ijpay.wxpay.WxPayApi;
import com.ijpay.wxpay.enums.WxDomainEnum;
import com.ijpay.wxpay.enums.v3.BasePayApiEnum;
import com.ijpay.wxpay.model.v3.RefundAmount;
import com.ijpay.wxpay.model.v3.RefundModel;

import cn.hutool.json.JSONUtil;
import jodd.util.StringPool;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 微信支付 正扫 反扫 通用处理
 * 
 * @author HlpayTeam
 * @date 2024/10/11 16:22
 */
@Slf4j
@Repository
@RequiredArgsConstructor
public class WxSceneCommonPayOrder {


    @Autowired
    private WxPayCommonParamsHandle wxPayCommonParamsHandle;

    @Value("${hlkj.domain}")
    private String domain;
    /**
     * 微信支付查询支付订单
     * 
     * @param channelPayOrderQueryContext
     */
    public void commonQueryChannelPayOrder(ChannelPayOrderQueryContext channelPayOrderQueryContext) {

        log.info("微信订单查询 请求参数:{}", JsonUtils.toJsonString(channelPayOrderQueryContext));
        // 判断商户类型
        MerchantAppChannelDO merchantAppChannelDO = channelPayOrderQueryContext.getMerchantAppChannelDO();
        boolean common = false;
        if(merchantAppChannelDO.getType() != null && MerchantEnum.APP_CHANNEL_TYPE.MERCHANT_COMMON.getCode()  == merchantAppChannelDO.getType()){
            common = true;
        }
        // 微信配置信息
        WxPayConfig wxPayConfig = null;
        // 普通商户
        if(common){
            wxPayConfig = JsonUtils.parseObject(merchantAppChannelDO.getAppConfigJson(), WxPayConfig.class);
            if(wxPayConfig == null){
                channelPayOrderQueryContext.setRemark("普通商户未配置通道参数");
                channelPayOrderQueryContext.setProcessStatus(false);
                return;
            }
        }else{
            CommonResult<WxPayConfig> wxPayConfigCommonResult = wxPayCommonParamsHandle.wxPayConfig(channelPayOrderQueryContext.getProviderChannelConfigDO(),
                    channelPayOrderQueryContext.isTest());
            if (!wxPayConfigCommonResult.isSuccess()) {
                channelPayOrderQueryContext.setRemark(wxPayConfigCommonResult.getMsg());
                channelPayOrderQueryContext.setProcessStatus(false);
                return;
            }
            wxPayConfig = wxPayConfigCommonResult.getData();
        }
        PayOrderDO payOrderDO = channelPayOrderQueryContext.getPayOrderDO();
        WxAppChannelParams wxAppChannelParams = JsonUtils.parseObject(merchantAppChannelDO.getAppConfigJson(), WxAppChannelParams.class);
        try {
            // 请求参数封装
            Map<String, String> params = new HashMap<>(16);
            // 请求地址
            String url = null;
            // 普通商户
            if(common){
                params.put("mchid", wxPayConfig.getMchId());
                url = String.format(BasePayApiEnum.ORDER_QUERY_BY_OUT_TRADE_NO.toString(), payOrderDO.getPayOrderNo());
                log.info("普通商户微信订单查询 请求params参数:{}", JSONUtil.toJsonStr(params));
            }else{
                params.put("sp_mchid", wxPayConfig.getMchId());
                params.put("sub_mchid", wxAppChannelParams.getSubMchId());
                url = String.format(BasePayApiEnum.PARTNER_ORDER_QUERY_BY_OUT_TRADE_NO.toString(), payOrderDO.getPayOrderNo());
                log.info("服务商微信订单查询 请求params参数:{}", JSONUtil.toJsonStr(params));
            }

            IJPayHttpResponse response = WxPayApi.v3(
                    RequestMethodEnum.GET,
                    WxDomainEnum.CHINA.toString(),
                    url,
                    wxPayConfig.getMchId(),
                    wxPayConfig.getSerialNo(),
                    wxPayConfig.getPayPublicId(),
                    wxPayConfig.getPrivateKeyPath(),
                    params,
                    AuthTypeEnum.RSA.getCode()
            );
            log.info("微信订单查询 响应参数:{}", response);
            boolean verifySignature = WxPayKit.verifyPublicKeySignature(response, wxPayConfig.getPayPublicPath());
            if(!verifySignature){
                channelPayOrderQueryContext.setProcessStatus(Boolean.FALSE);
                channelPayOrderQueryContext.setRemark("微信支付公钥验签失败");
                return;
            }
            WxNotifyResp respData = JsonUtils.parseObject(response.getBody(), WxNotifyResp.class);
            if (response.getStatus() == WxPayExtConstant.OK) {
                PayOrderDO updartePayOrderDO = new PayOrderDO();
                switch (respData.getTrade_state()) {
                    case "NOTPAY":
                    case "USERPAYING":
                        updartePayOrderDO.setState(PayOrderEnums.PAY_ORDER_STATUS.PAYING.getCode());
                        updartePayOrderDO.setBankMsg("");
                        break;
                    case "REVOKED":
                        updartePayOrderDO.setState(PayOrderEnums.PAY_ORDER_STATUS.CANCEL.getCode());
                        break;
                    case "REFUND":
                    case "SUCCESS":
                        updartePayOrderDO.setState(PayOrderEnums.PAY_ORDER_STATUS.SUCCESS.getCode());
                        updartePayOrderDO.setBankMsg("");
                        break;
                    case "PAYERROR":
                        updartePayOrderDO.setState(PayOrderEnums.PAY_ORDER_STATUS.FAIL.getCode());
                        break;
                    case "CLOSED":
                        updartePayOrderDO.setState(PayOrderEnums.PAY_ORDER_STATUS.CLOSE.getCode());
                        break;
                }

                if (StringUtils.hasText(respData.getSuccess_time())) {
                    try {
                        updartePayOrderDO.setSuccessTime(DateUtils.shanghaiDate(respData.getSuccess_time(), DateTimeFormatter.ISO_OFFSET_DATE_TIME));
                    }
                    catch (Exception e) {
                        log.error("微信订单查询返回 时间格式化错误 dateTime:{},message:{}", respData.getSuccess_time(), e.getMessage());
                    }
                }
                updartePayOrderDO.setChannelOrderNo(respData.getTransaction_id());
                if (respData.getAmount() != null) {
                    updartePayOrderDO.setSettlementAmount(MerchantUtils.fenToYuan(respData.getAmount().getTotal()));
                    updartePayOrderDO.setPayAmount(MerchantUtils.fenToYuan(respData.getAmount().getPayer_total()));
                }

                if (respData.getPayer() != null) {
                    updartePayOrderDO.setUserId(respData.getPayer().getSp_openid());
                }
                channelPayOrderQueryContext.setUpdartePayOrderDO(updartePayOrderDO);

            }else{
                String respDesc = respData.getTrade_state_desc();
                channelPayOrderQueryContext.setProcessStatus(Boolean.FALSE);
                if (StringUtils.hasText(respDesc)) {
                    channelPayOrderQueryContext.setRemark(respDesc);
                }
                else {
                    channelPayOrderQueryContext.setRemark("微信提交支付订单查询返回错误信息为空,错误码为:" + response.getStatus());
                }
            }

        }catch (Exception e){
            e.printStackTrace();
            log.error("微信查询支付订单请求失败 message:{}", e.getMessage(), e);
            channelPayOrderQueryContext.setRemark("微信查询支付订单请求失败,失败信息" + e.getMessage());
            channelPayOrderQueryContext.setProcessStatus(Boolean.FALSE);
        }

    }

    /**
     * 微信支付关闭订单
     * 
     * @param channelPayOrderQueryContext
     */
    public void commonCloseChannelPayOrder(ChannelPayOrderQueryContext channelPayOrderQueryContext) {
        log.info("微信关闭支付订单 请求参数:{}", JsonUtils.toJsonString(channelPayOrderQueryContext));
        // 判断商户类型
        MerchantAppChannelDO merchantAppChannelDO = channelPayOrderQueryContext.getMerchantAppChannelDO();
        boolean common = false;
        if(merchantAppChannelDO.getType() != null && MerchantEnum.APP_CHANNEL_TYPE.MERCHANT_COMMON.getCode()  == merchantAppChannelDO.getType()){
            common = true;
        }
        // 微信配置信息
        WxPayConfig wxPayConfig = null;
        // 普通商户
        if(common){
            wxPayConfig = JsonUtils.parseObject(merchantAppChannelDO.getAppConfigJson(), WxPayConfig.class);
            if(wxPayConfig == null){
                channelPayOrderQueryContext.setRemark("普通商户未配置通道参数");
                channelPayOrderQueryContext.setProcessStatus(false);
                return;
            }
        }else{
            CommonResult<WxPayConfig> wxPayConfigCommonResult = wxPayCommonParamsHandle.wxPayConfig(channelPayOrderQueryContext.getProviderChannelConfigDO(),
                    channelPayOrderQueryContext.isTest());
            if (!wxPayConfigCommonResult.isSuccess()) {
                channelPayOrderQueryContext.setRemark(wxPayConfigCommonResult.getMsg());
                channelPayOrderQueryContext.setProcessStatus(false);
                return;
            }
            wxPayConfig = wxPayConfigCommonResult.getData();
        }
        PayOrderDO payOrderDO = channelPayOrderQueryContext.getPayOrderDO();
        WxAppChannelParams wxAppChannelParams = JsonUtils.parseObject(merchantAppChannelDO.getAppConfigJson(), WxAppChannelParams.class);
        try {
            // 请求参数封装
            Map<String, String> params = new HashMap<>(16);
            // 请求地址
            String url = null;
            // 普通商户
            if(common){
                params.put("mchid", wxPayConfig.getMchId());
                url = String.format(BasePayApiEnum.CLOSE_ORDER_BY_OUT_TRADE_NO.toString(), payOrderDO.getPayOrderNo());
                log.info("普通商户微信关闭支付订单 请求params参数:{}", JSONUtil.toJsonStr(params));
            }else{
                params.put("sp_mchid", wxPayConfig.getMchId());
                params.put("sub_mchid", wxAppChannelParams.getSubMchId());
                params.put("out_trade_no", payOrderDO.getPayOrderNo());
                url = String.format(BasePayApiEnum.PARTNER_CLOSE_ORDER_BY_OUT_TRADE_NO.toString(), payOrderDO.getPayOrderNo());
                log.info("服务商微信关闭支付订单 请求params参数:{}", JSONUtil.toJsonStr(params));
            }
            IJPayHttpResponse response = WxPayApi.v3(
                    RequestMethodEnum.GET,
                    WxDomainEnum.CHINA.toString(),
                    url,
                    wxPayConfig.getMchId(),
                    wxPayConfig.getSerialNo(),
                    wxPayConfig.getPayPublicId(),
                    wxPayConfig.getPrivateKeyPath(),
                    params,
                    AuthTypeEnum.RSA.getCode()
            );
            log.info("微信关闭支付订单 响应参数:{}", response);
            boolean verifySignature = WxPayKit.verifyPublicKeySignature(response, wxPayConfig.getPayPublicPath());
            if(!verifySignature){
                channelPayOrderQueryContext.setProcessStatus(Boolean.FALSE);
                channelPayOrderQueryContext.setRemark("微信支付公钥验签失败");
                return;
            }
            if (response.getStatus() == WxPayExtConstant.OK) {
                PayOrderDO updartePayOrderDO = new PayOrderDO();
                updartePayOrderDO.setState(PayOrderEnums.PAY_ORDER_STATUS.CLOSE.getCode());
                channelPayOrderQueryContext.setUpdartePayOrderDO(updartePayOrderDO);
            }else{
                channelPayOrderQueryContext.setProcessStatus(Boolean.FALSE);
                channelPayOrderQueryContext.setRemark(response.getBody());
            }
        }catch (Exception e){
            log.error("微信关闭支付订单请求失败 message:{}", e);
            channelPayOrderQueryContext.setRemark("微信查询支付订单请求失败,失败信息" + e.getMessage());
            channelPayOrderQueryContext.setProcessStatus(Boolean.FALSE);
        }
    }

    /**
     * 微信支付查询关闭订单状态
     * 
     * @param channelPayOrderQueryContext
     */
    public void commonQueryChannelClosePayOrder(ChannelPayOrderQueryContext channelPayOrderQueryContext) {
        PayOrderDO updartePayOrderDO = new PayOrderDO();
        updartePayOrderDO.setState(PayOrderEnums.PAY_ORDER_STATUS.CLOSE.getCode());
        channelPayOrderQueryContext.setUpdartePayOrderDO(updartePayOrderDO);
    }

    /**
     * 微信支付订单退款
     * 
     * @param channelRefundPayOrderContext
     */
    public void commonRefundChannelPayOrder(ChannelRefundPayOrderContext channelRefundPayOrderContext) {

        log.info("微信支付退款 请求参数:{}", JsonUtils.toJsonString(channelRefundPayOrderContext));
        // 判断商户类型
        MerchantAppChannelDO merchantAppChannelDO = channelRefundPayOrderContext.getMerchantAppChannelDO();
        boolean common = false;
        if(merchantAppChannelDO.getType() != null && MerchantEnum.APP_CHANNEL_TYPE.MERCHANT_COMMON.getCode()  == merchantAppChannelDO.getType()){
            common = true;
        }
        // 微信配置信息
        WxPayConfig wxPayConfig = null;
        // 普通商户
        if(common){
            wxPayConfig = JsonUtils.parseObject(merchantAppChannelDO.getAppConfigJson(), WxPayConfig.class);
            if(wxPayConfig == null){
                channelRefundPayOrderContext.setRemark("普通商户未配置通道参数");
                channelRefundPayOrderContext.setProcessStatus(false);
                return;
            }
        }else{
            CommonResult<WxPayConfig> wxPayConfigCommonResult = wxPayCommonParamsHandle.wxPayConfig(channelRefundPayOrderContext.getProviderChannelConfigDO(),
                    channelRefundPayOrderContext.isTest());
            if (!wxPayConfigCommonResult.isSuccess()) {
                channelRefundPayOrderContext.setRemark(wxPayConfigCommonResult.getMsg());
                channelRefundPayOrderContext.setProcessStatus(false);
                return;
            }
            wxPayConfig = wxPayConfigCommonResult.getData();
        }
        PayOrderDO payOrderDO = channelRefundPayOrderContext.getPayOrderDO();
        RefundOrderDO refundOrderDO = channelRefundPayOrderContext.getRefundOrderDO();
        WxAppChannelParams wxAppChannelParams = JsonUtils.parseObject(merchantAppChannelDO.getAppConfigJson(), WxAppChannelParams.class);
        try {
            // 请求地址
            String url = null;
            // 请求参数封装
            RefundAmount refundAmount = new RefundAmount()
                    .setRefund(MerchantUtils.YuanToFen(refundOrderDO.getRefundAmount()))
                    .setTotal(MerchantUtils.YuanToFen(refundOrderDO.getAmount()))
                    .setCurrency("CNY");
            RefundModel refundModel = new RefundModel()
                    .setOut_trade_no(payOrderDO.getPayOrderNo())
                    .setOut_refund_no(refundOrderDO.getRefundOrderNo())
                    .setReason(refundOrderDO.getRefundReason())
                    .setAmount(refundAmount);
            // 普通商户
            if(common){
                url = BasePayApiEnum.REFUND.toString();
                // 商户回调通知
                String notifyUrl = domain + MerchantExtConstant.MERCHANT_REFUND_COMMON_ASYNC_CALLBACK_URL + payOrderDO.getChannelCode() + StringPool.SLASH + payOrderDO.getProviderCode()+ StringPool.SLASH + merchantAppChannelDO.getCode();
                refundModel.setNotify_url(notifyUrl);
                log.info("普通商户微信支付退款 请求params参数:{}", JSONUtil.toJsonStr(refundModel));
            }else{
                refundModel.setSub_mchid(wxAppChannelParams.getSubMchId()).setNotify_url(channelRefundPayOrderContext.getNotifyUrl());
                url = BasePayApiEnum.REFUND.toString();
                log.info("服务商微信支付退款 请求params参数:{}", JSONUtil.toJsonStr(refundModel));
            }

            IJPayHttpResponse response = WxPayApi.v3(
                    RequestMethodEnum.POST,
                    WxDomainEnum.CHINA.toString(),
                    url,
                    wxPayConfig.getMchId(),
                    wxPayConfig.getSerialNo(),
                    wxPayConfig.getPayPublicId(),
                    wxPayConfig.getPrivateKeyPath(),
                    JSONUtil.toJsonStr(refundModel),
                    AuthTypeEnum.RSA.getCode()
            );
            log.info("微信支付退款 响应参数:{}", response);
            boolean verifySignature = WxPayKit.verifyPublicKeySignature(response, wxPayConfig.getPayPublicPath());
            if(!verifySignature){
                channelRefundPayOrderContext.setProcessStatus(Boolean.FALSE);
                channelRefundPayOrderContext.setRemark("微信支付公钥验签失败");
                return;
            }
            RefundOrderDO updateRefundOrderDO = new RefundOrderDO();
            if (response.getStatus() == WxPayExtConstant.OK) {
                WxRefundOrderResp respData = JsonUtils.parseObject(response.getBody(), WxRefundOrderResp.class);
                switch (respData.getStatus()){
                    case "SUCCESS":
                        updateRefundOrderDO.setRefundStatus(PayOrderEnums.REFUND_STATUS.REFUND.getCode());
                        updateRefundOrderDO.setActualRefundAmount(MerchantUtils.fenToYuan(respData.getAmount().getPayer_total()));
                        updateRefundOrderDO.setSuccessTime(DateUtils.shanghaiDate(respData.getSuccess_time(), DateTimeFormatter.ISO_OFFSET_DATE_TIME));
                        break;
                    case "PROCESSING":
                        updateRefundOrderDO.setRefundStatus(PayOrderEnums.REFUND_STATUS.REFUNDING.getCode());
                        break;
                    case "CLOSED":
                    case "ABNORMAL":
                        updateRefundOrderDO.setRefundStatus(PayOrderEnums.REFUND_STATUS.FAIL.getCode());
                        break;
                }
                channelRefundPayOrderContext.setUpdateRefundOrderDO(updateRefundOrderDO);
            }else{
                channelRefundPayOrderContext.setProcessStatus(Boolean.FALSE);
                channelRefundPayOrderContext.setRemark(response.getBody());
            }

        }catch (Exception e){
            log.error("微信支付退款请求失败 message:{}", e);
            channelRefundPayOrderContext.setRemark("微信支付退款请求失败,失败信息" + e.getMessage());
            channelRefundPayOrderContext.setProcessStatus(Boolean.FALSE);
        }
    }

    /**
     * 微信支付订单退款状态查询
     * 
     * @param channelRefundPayOrderContext
     */
    public void commonQueryRefundChannelPayOrder(ChannelRefundPayOrderContext channelRefundPayOrderContext) {

        log.info("微信支付查询退款 请求参数:{}", JsonUtils.toJsonString(channelRefundPayOrderContext));
        // 判断商户类型
        MerchantAppChannelDO merchantAppChannelDO = channelRefundPayOrderContext.getMerchantAppChannelDO();
        boolean common = false;
        if(merchantAppChannelDO.getType() != null && MerchantEnum.APP_CHANNEL_TYPE.MERCHANT_COMMON.getCode()  == merchantAppChannelDO.getType()){
            common = true;
        }
        // 微信配置信息
        WxPayConfig wxPayConfig = null;
        // 普通商户
        if(common){
            wxPayConfig = JsonUtils.parseObject(merchantAppChannelDO.getAppConfigJson(), WxPayConfig.class);
            if(wxPayConfig == null){
                channelRefundPayOrderContext.setRemark("普通商户未配置通道参数");
                channelRefundPayOrderContext.setProcessStatus(false);
                return;
            }
        }else{
            CommonResult<WxPayConfig> wxPayConfigCommonResult = wxPayCommonParamsHandle.wxPayConfig(channelRefundPayOrderContext.getProviderChannelConfigDO(),
                    channelRefundPayOrderContext.isTest());
            if (!wxPayConfigCommonResult.isSuccess()) {
                channelRefundPayOrderContext.setRemark(wxPayConfigCommonResult.getMsg());
                channelRefundPayOrderContext.setProcessStatus(false);
                return;
            }
            wxPayConfig = wxPayConfigCommonResult.getData();
        }
        RefundOrderDO refundOrderDO = channelRefundPayOrderContext.getRefundOrderDO();
        WxAppChannelParams wxAppChannelParams = JsonUtils.parseObject(merchantAppChannelDO.getAppConfigJson(), WxAppChannelParams.class);
        try {
            // 请求参数封装
            Map<String, String> params = null;
            // 请求地址
            String url = null;
            // 普通商户
            if(common){
                url = String.format(BasePayApiEnum.REFUND_QUERY_BY_OUT_REFUND_NO.toString(), refundOrderDO.getRefundOrderNo());
                log.info("普通商户微信订单查询退款 请求params参数:{}", JSONUtil.toJsonStr(params));
            }else{
                params = new HashMap<>();
                params.put("sub_mchid", wxAppChannelParams.getSubMchId());
                url = String.format(BasePayApiEnum.REFUND_QUERY_BY_OUT_REFUND_NO.toString(), refundOrderDO.getRefundOrderNo());
                log.info("服务商微信订单查询退款 请求params参数:{}", JSONUtil.toJsonStr(params));
            }

            IJPayHttpResponse response = WxPayApi.v3(
                    RequestMethodEnum.GET,
                    WxDomainEnum.CHINA.toString(),
                    url,
                    wxPayConfig.getMchId(),
                    wxPayConfig.getSerialNo(),
                    wxPayConfig.getPayPublicId(),
                    wxPayConfig.getPrivateKeyPath(),
                    params,
                    AuthTypeEnum.RSA.getCode()
            );
            log.info("微信支付查询退款 响应参数:{}", response);
            boolean verifySignature = WxPayKit.verifyPublicKeySignature(response, wxPayConfig.getPayPublicPath());
            if(!verifySignature){
                channelRefundPayOrderContext.setProcessStatus(Boolean.FALSE);
                channelRefundPayOrderContext.setRemark("微信支付公钥验签失败");
                return;
            }
            RefundOrderDO updateRefundOrderDO = new RefundOrderDO();
            if (response.getStatus() == WxPayExtConstant.OK) {
                WxRefundOrderResp respData = JsonUtils.parseObject(response.getBody(), WxRefundOrderResp.class);
                switch (respData.getStatus()){
                    case "SUCCESS":
                        updateRefundOrderDO.setRefundStatus(PayOrderEnums.REFUND_STATUS.REFUND.getCode());
                        updateRefundOrderDO.setSuccessTime(DateUtils.shanghaiDate(respData.getSuccess_time(), DateTimeFormatter.ISO_OFFSET_DATE_TIME));
                        break;
                    case "PROCESSING":
                        updateRefundOrderDO.setRefundStatus(PayOrderEnums.REFUND_STATUS.REFUNDING.getCode());
                        break;
                    case "CLOSED":
                    case "ABNORMAL":
                        updateRefundOrderDO.setRefundStatus(PayOrderEnums.REFUND_STATUS.FAIL.getCode());
                        break;
                }
            }else{
                channelRefundPayOrderContext.setProcessStatus(Boolean.FALSE);
                channelRefundPayOrderContext.setRemark(response.getBody());
            }
            channelRefundPayOrderContext.setUpdateRefundOrderDO(updateRefundOrderDO);

        }catch (Exception e){
            log.error("微信支付查询退款请求失败 message:{}",  e);
            channelRefundPayOrderContext.setRemark("微信支付查询退款请求失败,失败信息" + e.getMessage());
            channelRefundPayOrderContext.setProcessStatus(Boolean.FALSE);
        }

    }


    public Integer expireMinute(Long time) {
        if (time == null) {
            return 30;
        }
        return (int) (time - System.currentTimeMillis()) / 1000 / 60;
    }


    public String reqTime() {
        return DateUtils.getDateString("yyyyMMddHHmmss");
    }

    public String openapiDomain(boolean test) {
        // 测试环境：https://test.wsmsd.cn/sit/api/v2/mms/openApi/wechatParamConf
        // 生产环境：https://s2.lakala.com/api/v2/mms/openApi/wechatParamConf
        if (test) {
            return "https://test.wsmsd.cn/sit";
        }
        else {
            return "https://s2.lakala.com";
        }
    }
}
